#include <vector>

using namespace std;

/**
 * Definition for a binary tree node.
 */
struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

class Solution {
public:
    bool isSymmetric(TreeNode* root) {
        TreeNode* copy = copy_and_flip(root);
        return compare(root, copy);
    }

    TreeNode* copy_and_flip(TreeNode* root) {
        if (!root) return nullptr;
        TreeNode* node = new TreeNode(root->val);
        node->left = copy_and_flip(root->right);
        node->right = copy_and_flip(root->left);
        return node;
    }

    bool compare(TreeNode* tree1, TreeNode* tree2) {
        if (!tree1 && !tree2) return true;
        if (!tree1 || !tree2) return false;
        if (tree1->val != tree2->val) return false;
        return compare(tree1->left, tree2->left) && compare(tree1->right, tree2->right);
    }
};
